#include <iostream>
#include <vector>
#include <math.h>
#include "bench.hpp"
#include "parse.hpp"

std::size_t N = 1 << 25;

struct SumTest
{
    double sum;
    std::vector<double> a;
    
    SumTest()
    : a(N, -1)
    {
    }

    void operator()()
    {
        sum = 0;
	double suma = 0;
	double sumb = 0;
	double sumc = 0;
	double sumd = 0;
	int looptimes = floor(N/4.0);
	// Uitgerolde lus
    for(std::size_t i = 0; i <= looptimes; ++i)
	{
        suma += a[i*4];
	    sumb += a[i*4+1];
	    sumc += a[i*4+2];
	    sumd += a[i*4+3];
	}
	sum = suma + sumb + sumc + sumd;
	// Eventuele niet-uitgerolde elementen
	for(std::size_t i=4*looptimes; i < N;++i)
	{
		sum += a[i];
	}
    }
    
    void use_result()
    {
        std::cout << sum << std::endl;
    }
    
};

int main(int argc,char *argv[])
{
    if(argc > 1) parse(argv[1], N);
    const size_t n = 100;      // number of experiments
    const size_t warmup = 60;  // number of warmup experiments for cache loads
    typedef TscTimer timer;   // use TSC timer
    Bench< SumTest, n, warmup, timer > b(std::cout);
    b.dump(std::cerr);        // output raw timing data to stderr
    return 0;
}
